package org.openurp.edu.student.info.web.action

import java.io.File

import org.beangle.commons.lang.Numbers
import org.beangle.data.dao.OqlBuilder
import org.beangle.webmvc.api.annotation.param
import org.beangle.webmvc.api.view.Stream
import org.beangle.webmvc.api.view.View
import org.beangle.webmvc.entity.action.RestfulAction
import org.openurp.edu.student.info.model.Schooling
import org.openurp.edu.student.info.model.Semester
import org.openurp.edu.student.info.model.Student
import org.openurp.edu.student.info.model.Visa
import org.openurp.edu.student.info.service.StudentPortraitService

class VisaAction extends RestfulAction[Visa] {
  var studentPortraitService: StudentPortraitService = _
  override protected def indexSetting(): Unit = {
    put("semesters", entityDao.getAll(classOf[Semester]).sortBy { semester => semester.id })
    val semesterQuery = OqlBuilder.from(classOf[Semester], "semester").where(":now between semester.beginOn and semester.endOn", new java.util.Date())
    put("currentSemester", entityDao.search(semesterQuery).head)
  }

  //  override protected def editSetting(entity: Schooling): Unit = {
  //    put("stdtypes", entityDao.getAll(classOf[Stdtype]))
  //    put("paystates", entityDao.getAll(classOf[Paystate]))
  //    put("paytypes", entityDao.getAll(classOf[Paytype]))
  //    put("scholarships", entityDao.getAll(classOf[Scholarship]))
  //    put("semesters", entityDao.getAll(classOf[Semester]).sortBy { semester => semester.id })
  //    val semesterQuery = OqlBuilder.from(classOf[Semester], "semester").where(":now between semester.beginOn and semester.endOn", new java.util.Date())
  //    put("currentSemester", entityDao.search(semesterQuery).head)
  //  }
  override def search(): String = {
    val uploaded = getLong("uploaded").getOrElse(1)
    val semesterId = Numbers.toInt(get("visa.semester.id").orNull, 0)
    val code = get("schooling.code")
    val name = get("schooling.name")

    val schoolingQue =
      OqlBuilder.from(classOf[Schooling], "schooling").where("schooling.semester.id=:id", semesterId)
        .limit(getPageLimit)
    code foreach (c => schoolingQue.where("schooling.code like :code", "%" + c + "%"))
    name foreach (c => schoolingQue.where("schooling.name like :name", "%" + c + "%"))
    schoolingQue.where("not exists(from "+classOf[Visa].getName+ " v,"+classOf[Student].getName +" s where s.studentWai = v.studentWai and s.code=schooling.code)")
    // 判断(签证是否上传)
    uploaded match {
      case 0 => //未上传--schoooling list
        put("schoolings", entityDao.search(schoolingQue))
        forward("schoolingList");
      case _ => //已上传 visa list
        val builder = getQueryBuilder()
        code foreach (c => builder.where(" exists(from " + classOf[Student].getName + " s where s.studentWai = visa.studentWai and s.code like :code)", "%" + c + "%"))
        name foreach (c => builder.where(" exists(from " + classOf[Student].getName + " s where s.studentWai = visa.studentWai and s.name like :name)", "%" + c + "%"))

        val visas = entityDao.search(builder)
        val wais = visas.map { x => x.studentWai }
        val students =
          if (wais.isEmpty) List.empty[Student] else
            entityDao.search(OqlBuilder.from(classOf[Student], "student").where("student.studentWai in (:studentWais)", wais))
        val studentMap = students.map(x => (x.studentWai, x)).toMap
        put("studentMap", studentMap)
        put("visas", visas)
        forward("list");
    }
  }

  def visaPhoto(): String = {
    val visaId = Numbers.toLong(get("visa.id").orNull, 0L)
    val visa = entityDao.get(classOf[Visa], visaId)
    put("visa", visa)
    forward()
  }

  def portrait(@param("visa.id") id: String): View = {
    val visaId = Numbers.toLong(id)
    val visa = entityDao.get(classOf[Visa], visaId)
    Stream(new File(studentPortraitService.headPortaitBase + visa.path))
  }

  def visaDate(): String = {
    val visaId = Numbers.toLong(get("visa.id").orNull, 0L)
    val visa = entityDao.get(classOf[Visa], visaId)
    put("visa", visa)
    forward()

  }
  def saveDate(@param("visa.id") id: String): View = {
    val visaId = Numbers.toLong(id)
    val visa = entityDao.get(classOf[Visa], visaId)
    val visaDate = getDate("visa.visaDate").get
    visa.visaDate = visaDate
    entityDao.saveOrUpdate(visa)
    redirect("search", "info.save.success")
  }
}

